package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


import com.probox.common.core.enums.CustomerDefinitionUnitEnum;
import com.probox.common.core.enums.CustomerGroupTypeEnum;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.customer.api.domain.CustomerGroupDefinitionQuery;
import com.probox.common.entity.domain.customer.CustomerFund;
import com.probox.common.entity.domain.customer.CustomerInfo;
import com.probox.common.entity.domain.customer.CustomerLevelInfo;
import com.probox.common.entity.mapper.customer.CustomerFundMapper;
import com.probox.common.entity.mapper.customer.CustomerInfoMapper;
import com.probox.common.entity.mapper.customer.CustomerLevelInfoMapper;
import com.probox.common.entity.service.customer.ICustomerGroupDefinitionService;
import com.probox.common.entity.service.order.IOrderInfoService;
import com.probox.common.entity.service.product.IProductOperationDetailService;
import com.probox.common.entity.vo.customer.GroupDefinitionInfoQuery;
import com.probox.product.api.domain.ProductOptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.customer.CustomerGroupDefinitionMapper;
import com.probox.common.entity.domain.customer.CustomerGroupDefinition;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 客户分群定义信息Service业务层处理
 *
 * @author zsy
 * @date 2021-07-15
 */
@Service
@Transactional
public class CustomerGroupDefinitionServiceImpl extends ServiceImpl<CustomerGroupDefinitionMapper, CustomerGroupDefinition> implements ICustomerGroupDefinitionService {
    @Autowired
    private CustomerGroupDefinitionMapper customerGroupDefinitionMapper;

    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @Autowired
    private CustomerFundMapper customerFundMapper;

    @Autowired
    private CustomerLevelInfoMapper customerLevelInfoMapper;


    @Autowired
    private IOrderInfoService orderInfoService;

    @Resource(name = "stringRedisTemplate")
    private ValueOperations<String, String> sops;

    @Autowired
    private IProductOperationDetailService iProductOperationDetailService;

    /**
     * 查询客户分群定义信息
     *
     * @param definitionUid 客户分群定义信息ID
     * @return 客户分群定义信息
     */
    @Override
    public CustomerGroupDefinition selectCustomerGroupDefinitionById(Long definitionUid) {
        return this.getById(definitionUid);
    }

    /**
     * 查询客户分群定义信息列表
     *
     * @param customerGroupDefinition 客户分群定义信息
     * @return 客户分群定义信息
     */
    @Override
    public List<CustomerGroupDefinition> queryList(CustomerGroupDefinition customerGroupDefinition) {
        QueryWrapper<CustomerGroupDefinition> queryWrapper = Wrappers.query();
        queryWrapper.eq("PLATFORM_ID", SecurityUtils.getPlatformId());
        queryWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
        return customerGroupDefinitionMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户分群定义信息
     *
     * @param customerGroupDefinition 客户分群定义信息
     * @return 结果
     */
    @Override
    public boolean insertCustomerGroupDefinition(CustomerGroupDefinition customerGroupDefinition) {
        customerGroupDefinition.setPlatformId(SecurityUtils.getPlatformId());
        customerGroupDefinition.setDataStatus(DataStatus.valid.getKey());
        customerGroupDefinition.setCreateTime(DateUtil.date());
        customerGroupDefinition.setCreateName(SecurityUtils.getUsername());
        customerGroupDefinition.setCreateUser(SecurityUtils.getUserId());
        return this.save(customerGroupDefinition);
    }

    /**
     * 修改客户分群定义信息
     *
     * @param customerGroupDefinition 客户分群定义信息
     * @return 结果
     */
    @Override
    public boolean updateCustomerGroupDefinition(CustomerGroupDefinition customerGroupDefinition) {
        customerGroupDefinition.setUpdateName(SecurityUtils.getUsername());
        customerGroupDefinition.setUpdateUser(SecurityUtils.getUserId());
        customerGroupDefinition.setUpdateTime(DateUtil.date());
        return this.updateById(customerGroupDefinition);
    }

    /**
     * 批量删除客户分群定义信息
     *
     * @param definitionUids 需要删除的客户分群定义信息ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerGroupDefinitionByIds(Long[] definitionUids) {
        UpdateWrapper<CustomerGroupDefinition> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("DEFINITION_UID", Arrays.asList(definitionUids));
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        return this.update(updateWrapper);
//        return this.removeByIds(Arrays.asList(definitionUids));
    }

    /**
     * 根据条件查询特征人群
     */
    @Override
    public List selectConditionCustomerInfo(List<GroupDefinitionInfoQuery> groupDefinitionInfoQuerie) {
        List customerUid = new ArrayList();
        HashMap customerMap = new HashMap();
        for (GroupDefinitionInfoQuery definitions : groupDefinitionInfoQuerie) {
            Long groupDefinitionUid = definitions.getDefinitionUid();
            CustomerGroupDefinition customerGroupDefinition = customerGroupDefinitionMapper.selectById(groupDefinitionUid);
            if (customerGroupDefinition != null) {
                if (CustomerGroupTypeEnum.sex.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //性别
                    QueryWrapper<CustomerInfo> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("SEX", definitions.getDefinitionValue());
                    queryWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
                    queryWrapper.eq("STATUS", DataStatus.valid.getKey());
                    queryWrapper.select("CUSTOMER_UID");
                    List<CustomerInfo> customerInfoList = customerInfoMapper.selectList(queryWrapper);
                    for (CustomerInfo info : customerInfoList) {
                        customerUid.add(info.getCustomerUid());
                    }
                }
                if (CustomerGroupTypeEnum.integral.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //积分
                    if (CustomerDefinitionUnitEnum.ge.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //大于
                        QueryWrapper<CustomerFund> fundQueryWrapper = new QueryWrapper<>();
                        fundQueryWrapper.ge("ALL_SCORE", definitions.getDefinitionValue());
                        fundQueryWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
                        fundQueryWrapper.select("CUSTOMER_UID");
                        List<CustomerFund> customerFunds = customerFundMapper.selectList(fundQueryWrapper);
                        for (CustomerFund funds : customerFunds) {
                            customerUid.add(funds.getCustomerUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.le.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //小于
                        QueryWrapper<CustomerFund> fundQueryWrapper = new QueryWrapper<>();
                        fundQueryWrapper.le("ALL_SCORE", definitions.getDefinitionValue());
                        fundQueryWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
                        fundQueryWrapper.select("CUSTOMER_UID");
                        List<CustomerFund> customerFunds = customerFundMapper.selectList(fundQueryWrapper);
                        for (CustomerFund funds : customerFunds) {
                            customerUid.add(funds.getCustomerUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.section.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //区间
                        QueryWrapper<CustomerFund> fundQueryWrapper = new QueryWrapper<>();
                        fundQueryWrapper.ge("ALL_SCORE", definitions.getDefinitionValue());
                        fundQueryWrapper.le("ALL_SCORE", definitions.getDefinitionSectionvalue());
                        fundQueryWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
                        fundQueryWrapper.select("CUSTOMER_UID");
                        List<CustomerFund> customerFunds = customerFundMapper.selectList(fundQueryWrapper);
                        for (CustomerFund funds : customerFunds) {
                            customerUid.add(funds.getCustomerUid());
                        }
                    }
                }
                if (CustomerGroupTypeEnum.memberLevel.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //会员等级
                    List<String> list = new ArrayList<>();
                    if (ObjectUtil.isNotEmpty(definitions.getDefinitionSectionvalue())){
                        String[] sectionValue = definitions.getDefinitionSectionvalue().split("/");
                        for (int i = 0; i < sectionValue.length; i++) {
                            if (ObjectUtil.isNotEmpty(sectionValue[i])){
                                list.add(sectionValue[i]);
                            }
                        }
                    }
                    if (ObjectUtil.isNotEmpty(definitions.getDefinitionValue())){
                        String[] value = definitions.getDefinitionValue().split("/");
                        for (int i = 0; i < value.length; i++) {
                            if (ObjectUtil.isNotEmpty(value[i])){
                                list.add(value[i]);
                            }
                        }
                    }
                    if(ObjectUtil.isNotEmpty(list)){
                        QueryWrapper<CustomerLevelInfo> levelInfoQueryWrapper = new QueryWrapper<>();
                        levelInfoQueryWrapper.in("LEVEL_UID", list);
                        levelInfoQueryWrapper.eq("STATUS", DataStatus.valid.getKey());
                        levelInfoQueryWrapper.select("CUSTOMER_UID");
                        List<CustomerLevelInfo> customerLevelInfoList = customerLevelInfoMapper.selectList(levelInfoQueryWrapper);
                        for (CustomerLevelInfo levelInfos : customerLevelInfoList) {
                            customerUid.add(levelInfos.getCustomerUid());
                        }
                    }
                }
                //店铺有购买 店铺无购买 店铺有加购 店铺无加购  店铺有下单  店铺无下单
                if (CustomerGroupTypeEnum.shopHaveBuy.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopNotBuy.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopHaveAddBuy.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopNotAddBuy.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopHavePlaceOrder.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopNotPlaceOrder.getKey().equals(customerGroupDefinition.getDefinitionName())) {

                    groupDefinitionInfo(customerGroupDefinition.getDefinitionName(), definitions.getDefinitionValue(), customerUid);
                }

                if (CustomerGroupTypeEnum.validBugNumber.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.notRefundOrderNumber.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //有效购买次数  有效购买金额  无退款订单数
                    CustomerGroupDefinitionQuery customerGroupDefinitionQuery = new CustomerGroupDefinitionQuery();
                    customerGroupDefinitionQuery.setDefinitionType(customerGroupDefinition.getDefinitionName());
                    customerGroupDefinitionQuery.setPlatformId(SecurityUtils.getPlatformId());
                    if (CustomerDefinitionUnitEnum.ge.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //大于
                        customerGroupDefinitionQuery.setBeginValue(definitions.getDefinitionValue());
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }

                    }
                    if (CustomerDefinitionUnitEnum.le.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //小于
                        customerGroupDefinitionQuery.setEndValue(definitions.getDefinitionValue());
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.section.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //区间
                        customerGroupDefinitionQuery.setBeginValue(definitions.getDefinitionValue());
                        customerGroupDefinitionQuery.setEndValue(definitions.getDefinitionSectionvalue());
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                }

                if (CustomerGroupTypeEnum.validBuyMoney.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //有效购买金额
                    CustomerGroupDefinitionQuery customerGroupDefinitionQuery = new CustomerGroupDefinitionQuery();
                    customerGroupDefinitionQuery.setDefinitionType(CustomerGroupTypeEnum.validBuyMoney.getKey());
                    customerGroupDefinitionQuery.setPlatformId(SecurityUtils.getPlatformId());

                    customerGroupDefinitionQuery.setBeginValue(definitions.getDefinitionValue());
                    if (StringUtils.isEmpty(definitions.getDefinitionSectionvalue())) {
                        customerGroupDefinitionQuery.setEndValue(definitions.getDefinitionSectionvalue());
                    }
                    if (CustomerDefinitionUnitEnum.ge.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //大于
//                        customerGroupDefinitionQuery.setBeginValue(Long.parseLong(definitions.getDefinitionValue()));
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.le.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //小于
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo:productOptVoDataList){
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.section.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //区间
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                }

            }

        }
        if (groupDefinitionInfoQuerie.size() == 1) {
            List distinctList = (List)customerUid.stream().distinct().collect(Collectors.toList());
            return distinctList;
        } else {
            List uid =  getDuplicateElements(customerUid.stream());
            return uid;
//            List count = new ArrayList();
//            Set set = new HashSet();
//            set.addAll(customerUid);
//            Collection rs = org.apache.commons.collections.CollectionUtils.disjunction(customerUid, set);
//            Iterator it = rs.iterator();
//            while (it.hasNext()) {
//                Long uid = (Long) it.next();
//                count.add(uid);
//                return count;
//            }
        }
//        return null;
    }



    //查询店铺人群信息
    private List<Long> groupDefinitionInfo(String definitionName, String definitionValue, List customerUid) {
        DateTime date = DateUtil.date();
        int negativeVal = 0;
        if (!StringUtils.isEmpty(definitionValue)) {
            Integer value = Integer.valueOf(definitionValue);
            negativeVal = (~(value - 1));
        }
        DateTime beginTime = DateUtil.offsetDay(date, negativeVal);
        CustomerGroupDefinitionQuery customerGroupDefinitionQuery = new CustomerGroupDefinitionQuery();
        customerGroupDefinitionQuery.setBeginTime(beginTime);
        customerGroupDefinitionQuery.setEndTime(date);
        customerGroupDefinitionQuery.setDefinitionType(definitionName);
        customerGroupDefinitionQuery.setPlatformId(SecurityUtils.getPlatformId());

        List<com.probox.product.api.domain.ProductOptVo> productOptVoDataList = iProductOperationDetailService.getMemberUid(customerGroupDefinitionQuery);
//        List custoemrUidList = new ArrayList();
        if(productOptVoDataList != null){
            for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                customerUid.add(productOptVo.getMemberUid());
            }
        }
        return customerUid;
    }

    //查询店铺人群信息
    private List<Long> groupDefinitionInfoByPlatformId(String definitionName, String definitionValue, List customerUid,Long PlatformId) {
        DateTime date = DateUtil.date();
        int negativeVal = 0;
        if (!StringUtils.isEmpty(definitionValue)) {
            Integer value = Integer.valueOf(definitionValue);
            negativeVal = (~(value - 1));
        }
        DateTime beginTime = DateUtil.offsetDay(date, negativeVal);
        CustomerGroupDefinitionQuery customerGroupDefinitionQuery = new CustomerGroupDefinitionQuery();
        customerGroupDefinitionQuery.setBeginTime(beginTime);
        customerGroupDefinitionQuery.setEndTime(date);
        customerGroupDefinitionQuery.setDefinitionType(definitionName);
        customerGroupDefinitionQuery.setPlatformId(PlatformId);
        List<com.probox.product.api.domain.ProductOptVo> productOptVoDataList = iProductOperationDetailService.getMemberUid(customerGroupDefinitionQuery);
        if(productOptVoDataList != null){
            for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                customerUid.add(productOptVo.getMemberUid());
            }
        }
        return customerUid;
    }

    /**
     * 根据条件查询特征人群（指定平台）
     */
    @Override
    public List selectConditionCustomerInfoByPlatformId(List<GroupDefinitionInfoQuery> groupDefinitionInfoQuerie,Long platformId) {
        List customerUid = new ArrayList();
        HashMap customerMap = new HashMap();
        for (GroupDefinitionInfoQuery definitions : groupDefinitionInfoQuerie) {
            Long groupDefinitionUid = definitions.getDefinitionUid();
            CustomerGroupDefinition customerGroupDefinition = customerGroupDefinitionMapper.selectById(groupDefinitionUid);
            if (customerGroupDefinition != null) {
                if (CustomerGroupTypeEnum.sex.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //性别
                    QueryWrapper<CustomerInfo> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("SEX", definitions.getDefinitionValue());
                    queryWrapper.eq("PLATFORM_UID", platformId);
                    queryWrapper.eq("STATUS", DataStatus.valid.getKey());
                    queryWrapper.select("CUSTOMER_UID");
                    List<CustomerInfo> customerInfoList = customerInfoMapper.selectList(queryWrapper);
                    for (CustomerInfo info : customerInfoList) {
                        customerUid.add(info.getCustomerUid());
                    }
                }
                if (CustomerGroupTypeEnum.integral.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //积分
                    if (CustomerDefinitionUnitEnum.ge.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //大于
                        QueryWrapper<CustomerFund> fundQueryWrapper = new QueryWrapper<>();
                        fundQueryWrapper.ge("ALL_SCORE", definitions.getDefinitionValue());
                        fundQueryWrapper.eq("PLATFORM_UID", platformId);
                        fundQueryWrapper.select("CUSTOMER_UID");
                        List<CustomerFund> customerFunds = customerFundMapper.selectList(fundQueryWrapper);
                        for (CustomerFund funds : customerFunds) {
                            customerUid.add(funds.getCustomerUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.le.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //小于
                        QueryWrapper<CustomerFund> fundQueryWrapper = new QueryWrapper<>();
                        fundQueryWrapper.le("ALL_SCORE", definitions.getDefinitionValue());
                        fundQueryWrapper.eq("PLATFORM_UID", platformId);
                        fundQueryWrapper.select("CUSTOMER_UID");
                        List<CustomerFund> customerFunds = customerFundMapper.selectList(fundQueryWrapper);
                        for (CustomerFund funds : customerFunds) {
                            customerUid.add(funds.getCustomerUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.section.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //区间
                        QueryWrapper<CustomerFund> fundQueryWrapper = new QueryWrapper<>();
                        fundQueryWrapper.ge("ALL_SCORE", definitions.getDefinitionValue());
                        fundQueryWrapper.le("ALL_SCORE", definitions.getDefinitionSectionvalue());
                        fundQueryWrapper.eq("PLATFORM_UID", platformId);
                        fundQueryWrapper.select("CUSTOMER_UID");
                        List<CustomerFund> customerFunds = customerFundMapper.selectList(fundQueryWrapper);
                        for (CustomerFund funds : customerFunds) {
                            customerUid.add(funds.getCustomerUid());
                        }
                    }
                }
                if (CustomerGroupTypeEnum.memberLevel.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //会员等级
                    QueryWrapper<CustomerLevelInfo> levelInfoQueryWrapper = new QueryWrapper<>();
                    levelInfoQueryWrapper.eq("LEVEL_UID", definitions.getDefinitionValue());
                    levelInfoQueryWrapper.eq("STATUS", DataStatus.valid.getKey());
                    levelInfoQueryWrapper.select("CUSTOMER_UID");
                    List<CustomerLevelInfo> customerLevelInfoList = customerLevelInfoMapper.selectList(levelInfoQueryWrapper);
                    for (CustomerLevelInfo levelInfos : customerLevelInfoList) {
                        customerUid.add(levelInfos.getCustomerUid());
                    }
                }
                //店铺有购买 店铺无购买 店铺有加购 店铺无加购  店铺有下单  店铺无下单
                if (CustomerGroupTypeEnum.shopHaveBuy.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopNotBuy.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopHaveAddBuy.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopNotAddBuy.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopHavePlaceOrder.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.shopNotPlaceOrder.getKey().equals(customerGroupDefinition.getDefinitionName())) {

                    groupDefinitionInfoByPlatformId(customerGroupDefinition.getDefinitionName(), definitions.getDefinitionValue(), customerUid,platformId);
                }

                if (CustomerGroupTypeEnum.validBugNumber.getKey().equals(customerGroupDefinition.getDefinitionName())
                        || CustomerGroupTypeEnum.notRefundOrderNumber.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //有效购买次数  有效购买金额  无退款订单数
                    CustomerGroupDefinitionQuery customerGroupDefinitionQuery = new CustomerGroupDefinitionQuery();
                    customerGroupDefinitionQuery.setDefinitionType(customerGroupDefinition.getDefinitionName());
                    customerGroupDefinitionQuery.setPlatformId(platformId);
                    if (CustomerDefinitionUnitEnum.ge.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //大于
                        customerGroupDefinitionQuery.setBeginValue(definitions.getDefinitionValue());
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }

                    }
                    if (CustomerDefinitionUnitEnum.le.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //小于
                        customerGroupDefinitionQuery.setEndValue(definitions.getDefinitionValue());
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.section.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //区间
                        customerGroupDefinitionQuery.setBeginValue(definitions.getDefinitionValue());
                        customerGroupDefinitionQuery.setEndValue(definitions.getDefinitionSectionvalue());
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                }

                if (CustomerGroupTypeEnum.validBuyMoney.getKey().equals(customerGroupDefinition.getDefinitionName())) {
                    //有效购买金额
                    CustomerGroupDefinitionQuery customerGroupDefinitionQuery = new CustomerGroupDefinitionQuery();
                    customerGroupDefinitionQuery.setDefinitionType(CustomerGroupTypeEnum.validBuyMoney.getKey());
                    customerGroupDefinitionQuery.setPlatformId(platformId);

                    customerGroupDefinitionQuery.setBeginValue(definitions.getDefinitionValue());
                    if (StringUtils.isEmpty(definitions.getDefinitionSectionvalue())) {
                        customerGroupDefinitionQuery.setEndValue(definitions.getDefinitionSectionvalue());
                    }
                    if (CustomerDefinitionUnitEnum.ge.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //大于
//                        customerGroupDefinitionQuery.setBeginValue(Long.parseLong(definitions.getDefinitionValue()));
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.le.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //小于
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo:productOptVoDataList){
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                    if (CustomerDefinitionUnitEnum.section.getKey().equals(definitions.getDefinitionUnitvalue())) {
                        //区间
                        List<ProductOptVo> productOptVoDataList = orderInfoService.getOrderCount(customerGroupDefinitionQuery);
                        for (com.probox.product.api.domain.ProductOptVo productOptVo : productOptVoDataList) {
                            customerUid.add(productOptVo.getMemberUid());
                        }
                    }
                }

            }

        }
        if (groupDefinitionInfoQuerie.size() == 1) {
            List distinctList = (List)customerUid.stream().distinct().collect(Collectors.toList());
            return distinctList;
        } else {
            List uid =  getDuplicateElements(customerUid.stream());
            return uid;
//            List count = new ArrayList();
//            Set set = new HashSet();
//            set.addAll(customerUid);
//            Collection rs = org.apache.commons.collections.CollectionUtils.disjunction(customerUid, set);
//            Iterator it = rs.iterator();
//            while (it.hasNext()) {
//                Long uid = (Long) it.next();
//                count.add(uid);
//                return count;
//            }
        }
//        return null;
    }

    /**
     * 查询所有特征信息
     *
     * @return
     */
    @Override
    public List<CustomerGroupDefinition> getAllDefinitionInfo() {
        QueryWrapper<CustomerGroupDefinition> definitionQueryWrapper = new QueryWrapper<>();
        definitionQueryWrapper.eq("PLATFORM_ID", SecurityUtils.getPlatformId());
        definitionQueryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        List<CustomerGroupDefinition> customerGroupDefinitions = customerGroupDefinitionMapper.selectList(definitionQueryWrapper);
        return customerGroupDefinitions;
    }

    /**
     * 两个list取重复
     * @author
     * @param list1
     * @param list2
     * @return
     */
    public static List<Integer> getRepetition(List<Integer> list1,
                                              List<Integer> list2) {
        List<Integer> result = new ArrayList<Integer>();
        for (Integer integer : list2) {//遍历list1
            if (list1.contains(integer)) {//如果存在这个数
                result.add(integer);//放进一个list里面，这个list就是交集
            }
        }
        return result;
    }

    public static <T> List<T> getDuplicateElements(Stream<T> stream) {
        return stream.collect(Collectors.groupingBy(p -> p,Collectors.counting()))
                .entrySet().stream() // Set<Entry>转换为Stream<Entry>
                .filter(entry -> entry.getValue() > 1) // 过滤出元素出现次数大于 1 的 entry
                .map(entry -> entry.getKey()) // 获得 entry 的键（重复元素）对应的 Stream
                .collect(Collectors.toList()); // 转化为 List
    }

}
